package com.tyl.svolatile;

import java.util.concurrent.TimeUnit;

public class VolatileTest {
    //不加volatile程序会死循环
    //程序无法判断sum已经被修改了
    //加上volatile保证可见性
    private volatile static int sum=0;
    public static void main(String[] args) {
        new Thread(()->{
            while(sum==0){
            }
        }).start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println( sum=1);
    }
}
